<!-- 新增分区 -->
<template>
  <div>
    <el-dialog title="申请付款" :visible.sync="dialogShow" width="65%" :close-on-click-modal="false" :close-on-press-escape="false">
      <form-common :filed-list="applyFiledList" ref="applyForm"></form-common>
      <div class="flex margin-bottom-20">
        <p class="left-lable font-size-16 padding-left-right-10 margin-top-5 required font-weight-400 color-333333" style="width: 165px">审批文件</p>
        <uploadFile :is-multiple="true" :limit="10" filed-name="file" :max-size="20" accept-file-type=".xlsx,.xls,.csv,.png,.jpg,.Png,.Jpg,.mp4,.pdf" :file-list="fileList" @handleSuccess="handleSuccess" file-url-name="fileUrl" file-txt-name="fileTitle"> </uploadFile>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogShow = false" class="el-button-reset">取 消</el-button>
        <el-button type="primary" @click="confirmFn">确 认</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
  import formCommon from '@g/components/form/formCommon.vue';
  import { applyFiledList } from './applyPay.js';
  import { apiAddPayRecord } from '../../../api/contract';
  import { apiCreateAudit } from '../../../api/common';
  import uploadFile from '../../projectInfo/modules/upload.vue';
  import { requestTips } from '../../../assets/util';

  export default {
    props: {
      contractInfo: {
        type: Object,
        default: () => {
          return {};
        },
      },
      //是否是详情
      isInfo: {
        type: String,
        default: '',
      },
      unpaidAmount: {
        type: Number,
        default: 0
      },
      acceptanceList: {
        type: Array,
        default: () => {
          return [];
        },
      },
    },
    data() {
      return {
        dialogShow: false,
        applyFiledList: applyFiledList,
        fileList: [],
        userInfo:JSON.parse(localStorage.getItem('userInfo'))
      };
    },

    methods: {
      async confirmFn() {
        let model = this.$refs.applyForm.validateForm();
        if (!model) return
        console.log(this.fileList)
        model.projectKey = this.contractInfo.projectKey;
        model.orgKey = this.contractInfo.orgKey;
        model.applyUser = this.userInfo.key;
        model.contractKey = this.$route.query.contractKey
        model.contractCode = this.$route.query.contractCode
        model.attachmentList = this.fileList;
        model.recordStatus = 1;
        model.fundsType = 2 //(1,收款 2,付款)
        let res = await apiAddPayRecord(model);
        if (res.success) {
            this.submitFn(res.key);
          } else {
            this.$message.error(res.message)
          }
      },
      //初始化一些值
      initFieldVal(unpaidAmount,acceptanceList,chargeList) {
        console.log(this.$refs.applyForm);
        console.log(this.$refs);
        console.log(unpaidAmount);
        this.$refs.applyForm.initModel();
        this.applyFiledList[0].bindFieldVal = this.contractInfo.projectName;
        this.applyFiledList[1].bindFieldVal = this.contractInfo.orgName;
        this.applyFiledList[2].bindFieldVal = this.userInfo.nickname;
        this.applyFiledList[3].bindFieldVal = this.contractInfo.contractName;
        this.applyFiledList[4].bindFieldVal = unpaidAmount || '0';
        this.applyFiledList[6].optionList = chargeList;
        this.applyFiledList[7].optionList = acceptanceList;
      },
      //提审
      async submitFn(key) {
        let userInfo = JSON.parse(localStorage.getItem('userInfo'));
        let projectKey = this.$parent.getProjectKey();
        let param = {
          businessKey: key, //废止合同保存的主键
          userPrimaryKey: userInfo.key,
          projectKey: projectKey,
          type: '付款', //合同,账单,付款,合同终止
          typeKey: 'payment', //contractSubmit,bill,payment,contractTermination
        };
        let res = await apiCreateAudit(param);
        requestTips(res, async () => {
          this.$emit('getInfo');
          this.dialogShow = false;
        });
      },
      //上传成功
      handleSuccess(file, fileList) {
        if (file.data.rows && file.data.rows.length > 0) {
          this.fileList.push({
            fileId: file.data.rows[0].key,
            fileUrl: file.data.rows[0].url,
            fileTitle: file.data.rows[0].originalName,
            fileDesc: file.data.rows[0].originalName
          });
        } else {
          this.fileList = [];
          this.$message.error(file.data.message);
        }
        console.log(this.fileList)

      },
    },

    components: { formCommon, uploadFile },
  };
</script>
<style lang="scss" scoped></style>
